<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-08-08
 * Time: 15:01
 */

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Illuminate\Contracts\Auth\Factory as Auth;

class Apptoken
{

    /**
     * The authentication guard factory instance.
     *
     * @var \Illuminate\Contracts\Auth\Factory
     */
    protected $auth;

    /**
     * Create a new middleware instance.
     *
     * @param  \Illuminate\Contracts\Auth\Factory  $auth
     * @return void
     */
    public function __construct(Auth $auth)
    {
        $this->auth = $auth;
    }


    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        $id = Redis::get($request->input('apptoken'));
//        $id = DB::table('user')->where('u_app_token', $request->input('apptoken'))->value('u_id');
        if (!$id) {
            return ['code' => 401, 'msg' => 'Unauthorized'];
        }
        return $next($request);
    }
}